package yur.wedding.modules.sys.service.impl;

import com.itextpdf.text.log.SysoCounter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import yur.wedding.common.utils.Query;

import yur.wedding.modules.sys.dao.SysDocumentDirectoryrightsDao;
import yur.wedding.modules.sys.entity.SysDeptEntity;
import yur.wedding.modules.sys.entity.SysDocumentDirectoryrightsEntity;
import yur.wedding.modules.sys.entity.SysRoleEntity;
import yur.wedding.modules.sys.entity.SysUserEntity;
import yur.wedding.modules.sys.service.SysDeptService;
import yur.wedding.modules.sys.service.SysDocumentDirectoryrightsService;
import yur.wedding.modules.sys.service.SysRoleService;
import yur.wedding.modules.sys.service.SysUserService;
import yur.wedding.utils.PageUtils;

import javax.annotation.Resource;


@Service("sysDocumentDirectoryrightsService")
public class SysDocumentDirectoryrightsServiceImpl extends ServiceImpl<SysDocumentDirectoryrightsDao, SysDocumentDirectoryrightsEntity> implements SysDocumentDirectoryrightsService {

    @Resource
    SysUserService user;
    @Resource
    SysRoleService role;
    @Resource
    SysDeptService dept;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysDocumentDirectoryrightsEntity> page = this.page(
                new Query<SysDocumentDirectoryrightsEntity>().getPage(params),
                new QueryWrapper<SysDocumentDirectoryrightsEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取所在文档下面有那些权限
     *
     * @param dbgyId
     * @param params
     * @return
     */
    @Override
    public PageUtils documentDirectoryrightslist(Long dbgyId, Map<String, Object> params) {

        IPage<SysDocumentDirectoryrightsEntity> page = this.page(
                new Query<SysDocumentDirectoryrightsEntity>().getPage(params),
                new QueryWrapper<SysDocumentDirectoryrightsEntity>().eq(true, "dbgy_id", dbgyId)
        );
        for (SysDocumentDirectoryrightsEntity entity : page.getRecords()) {


            if (entity.getSharetype() == 1) {
                SysUserEntity byId = user.getById(entity.getUserid());
                entity.setName(byId.getLastName());

            }
            if (entity.getSharetype() == 2) {
                SysDeptEntity byId = dept.getById(entity.getDepartmentid());
                entity.setName(byId.getName());
            }
            if (entity.getSharetype() == 3) {
                SysRoleEntity byId = role.getById(entity.getRoleid());
                entity.setName(byId.getRoleName());
            }
            if (entity.getSharetype() == 4) {
                entity.setName("所有人");
            }


        }


        return new PageUtils(page);
    }

    /**
     * 文档添加权限
     *
     * @param userIdIds
     */
    @Override
    public void DirecToryrigsave(String userIdIds) {


        int foralluser = 0;//是否共享所有人  默认不共享所有人
        int downloadlevel = 1;//下载权限级别 默认可下载

        JSONObject jsonObject = JSONObject.fromObject(userIdIds);

        //共享类型(1人力资源，2部门，3角色，4所有人)
        String sharetype1 = (String) jsonObject.get("sharetype");
        Integer sharetype = Integer.valueOf(sharetype1);
        JSONArray jsonArr = null;
        if (sharetype.equals(1)) {
            jsonArr = (JSONArray) jsonObject.get("userIDs");
        } else if (sharetype.equals(2)) {
            jsonArr = (JSONArray) jsonObject.get("deptIDs");
        } else if (sharetype.equals(3)) {
            jsonArr = (JSONArray) jsonObject.get("roleIDs");
        }else  if (sharetype.equals(4)){
            //所以人 为空
        }

        //文档ID
        String dbgyId1 = (String) jsonObject.get("dbgyId");
        Long dbgyId = Long.valueOf(dbgyId1);

        //共享级别(	1：查看，2：编辑)
        String sharelevel1 = (String) jsonObject.get("sharelevel");
        Integer sharelevel = Integer.valueOf(sharelevel1);


        //是否共享所有人权限
        String foralluser1 = (String) jsonObject.get("foralluser");
        if (foralluser1 != null) {
            foralluser = Integer.valueOf(foralluser1);
        }

        Map<String, Object> map = new HashMap<>();
        if (!sharetype.equals(4)){
            Long useridList[] = new Long[jsonArr == null ? 0 : jsonArr.size()];
            if (jsonArr != null) {
                for (int i = 0; i < jsonArr.size(); i++) {
                    useridList[i] = Long.valueOf(jsonArr.get(i).toString());
                }
                map.put("List", useridList);//用户ID
            }
        }



        System.out.println("文档ID:" + dbgyId + ",是否可编辑权限:" + sharelevel + ",是否共享所有人权限:" + foralluser + ",共享类型:" + sharetype);
        map.put("dbgyId", dbgyId);//文档ID

        map.put("createDate", new Date());//创建时间
        map.put("sharetype", sharetype);//共享类型(1人力资源，2部门，3角色，4所有人)
        map.put("foralluser", foralluser);//是否共享所有人0：否，1：是
        map.put("downloadlevel", downloadlevel);//下载权限级别
        map.put("sharelevel", sharelevel);//共享级别
        if (sharetype.equals(1)) {
            System.out.println("执行添加文档用户权限");
            baseMapper.DirectoryrigUsersave(map);

        } else if (sharetype.equals(2)) {
            System.out.println("执行添加文档部门权限");
            baseMapper.DirectoryrigDepartmentsave(map);
        } else if (sharetype.equals(3)) {
            System.out.println("执行添加文档角色权限");
            baseMapper.Directoryrigrolesave(map);
        } else if (sharetype.equals(4)) {
            System.out.println("执行添加文档所有人权限");
            baseMapper.DirectoryrigOwnersave(map);
        }


    }


}
